package com.yanqu.road.server.manger.activity.fellingtrees;

import java.util.ArrayList;
import java.util.List;

public class FellingTreesEffectLogic {

    public static void main(String[] args) {
        int typeCount = 7;
        int height = 5;
        int width = 8;
        int index = 18;

        int[][] arrArr = new int[height][width];
        for (int type = 1; type <= typeCount; type++) {
            for (int direction = 1; direction <= 4; direction++) {
                System.err.println("类型" + type + "，方向" + direction + "：");
                List<Integer> effectIndexList = getEffectIndexList(type, direction, index, 5, 8);
                for (int y = height - 1; y >= 0; y--) {
                    for (int x = 0; x < width; x++) {
                        int tmpIndex = getIndexByXy(arrArr, x, y);
                        if (effectIndexList.contains(tmpIndex)) {
                            System.err.print("●");
                        } else if (tmpIndex == index) {
                            System.err.print("○");
                        } else {
                            System.err.print("□");
                        }
                    }
                    System.err.println("");
                }
                System.err.println("===================");
            }
        }

    }

    // =======================================================================================

    /**
     * 获取装备影响坐标
     *
     * @param type      词条类型
     * @param direction 方向
     * @param index     当前装备下标
     * @param height    装备栏高度
     * @param width     装备栏宽度
     * @return 下标
     */
    public static List<Integer> getEffectIndexList(int type, int direction, int index, int height, int width) {
        // 方向只有4个
        if (direction < 0 || direction > 4) {
            throw new RuntimeException();
        }
        // 当前坐标必须存在
        int[][] arrArr = new int[height][width];
        List<Integer> xy = getXyByIndex(arrArr, index);
        if (xy == null) {
            return new ArrayList<>();
        }
        int x = xy.get(0);
        int y = xy.get(1);

        // 【按类型处理】
        if (type == 1) {
            return getType1(arrArr, direction, x, y);
        } else if (type == 2) {
            return getType2(arrArr, x, y);
        } else if (type == 3) {
            return getType3(arrArr, x, y);
        } else if (type == 4) {
            return getType4(arrArr, direction, x, y);
        } else if (type == 5) {
            return getType5(arrArr, direction, x, y);
        } else if (type == 6) {
            return getType6(arrArr, direction, x, y);
        } else if (type == 7) {
            return getType7(arrArr, index);
        }

        // 未知type返回空
        return new ArrayList<>();
    }

    // 格式[x, y]，null表示不存在这个坐标
    private static List<Integer> getXyByIndex(int[][] arrArr, int index) {
        int x = index % arrArr[0].length;
        int y = arrArr.length - 1 - index / arrArr[0].length;
        if (!isXyExist(arrArr, x, y)) {
            return null;
        }
        List<Integer> list = new ArrayList<>();
        list.add(x);
        list.add(y);
        return list;
    }

    // -1表示不存在这个坐标
    private static int getIndexByXy(int[][] arrArr, int x, int y) {
        if (!isXyExist(arrArr, x, y)) {
            return -1;
        }
        int hang = arrArr.length - 1 - y;
        return hang * arrArr[0].length + x;
    }

    // 坐标是否存在
    private static boolean isXyExist(int[][] arrArr, int x, int y) {
        return y >= 0 && x >= 0 && y < arrArr.length && x < arrArr[0].length;
    }

    // =======================================================================================

    /**
     * 上方单个
     */
    private static List<Integer> getType1(int[][] arrArr, int direction, int x, int y) {
        List<Integer> resultList = new ArrayList<>();

        // 上、右、下、左
        int[] changeXArr = {0, 1, 0, -1};
        int[] changeYArr = {1, 0, -1, 0};

        x += changeXArr[direction - 1];
        y += changeYArr[direction - 1];
        int index = getIndexByXy(arrArr, x, y);
        if (index != -1) resultList.add(index);

        return resultList;
    }

    /**
     * 四个斜向单个
     */
    private static List<Integer> getType2(int[][] arrArr, int x, int y) {
        List<Integer> resultList = new ArrayList<>();

        int index1 = getIndexByXy(arrArr, x - 1, y + 1);
        int index2 = getIndexByXy(arrArr, x + 1, y + 1);
        int index3 = getIndexByXy(arrArr, x + 1, y - 1);
        int index4 = getIndexByXy(arrArr, x - 1, y - 1);
        if (index1 != -1) resultList.add(index1);
        if (index2 != -1) resultList.add(index2);
        if (index3 != -1) resultList.add(index3);
        if (index4 != -1) resultList.add(index4);

        return resultList;
    }

    /**
     * 周围一圈
     */
    private static List<Integer> getType3(int[][] arrArr, int x, int y) {
        List<Integer> resultList = new ArrayList<>();

        int index1 = getIndexByXy(arrArr, x - 1, y + 1);
        int index2 = getIndexByXy(arrArr, x, y + 1);
        int index3 = getIndexByXy(arrArr, x + 1, y + 1);
        int index4 = getIndexByXy(arrArr, x - 1, y);
        int index5 = getIndexByXy(arrArr, x + 1, y);
        int index6 = getIndexByXy(arrArr, x - 1, y - 1);
        int index7 = getIndexByXy(arrArr, x, y - 1);
        int index8 = getIndexByXy(arrArr, x + 1, y - 1);
        if (index1 != -1) resultList.add(index1);
        if (index2 != -1) resultList.add(index2);
        if (index3 != -1) resultList.add(index3);
        if (index4 != -1) resultList.add(index4);
        if (index5 != -1) resultList.add(index5);
        if (index6 != -1) resultList.add(index6);
        if (index7 != -1) resultList.add(index7);
        if (index8 != -1) resultList.add(index8);

        return resultList;
    }

    /**
     * 上方横向直线
     */
    private static List<Integer> getType4(int[][] arrArr, int direction, int x, int y) {
        List<Integer> resultList = new ArrayList<>();

        // 上、右、下、左
        int[] beginXArr = {-1, x + 1, -1, x - 1};
        int[] beginYArr = {y + 1, -1, y - 1, -1};
        int[] changeXArr = {1, 0, 1, 0};
        int[] changeYArr = {0, 1, 0, 1};

        int tempX = beginXArr[direction - 1];
        int tempY = beginYArr[direction - 1];
        for (int i = 0; i < 100; i++) {
            tempX += changeXArr[direction - 1];
            tempY += changeYArr[direction - 1];
            int tempIndex = getIndexByXy(arrArr, tempX, tempY);
            if (tempIndex == -1) {
                break;
            }
            resultList.add(tempIndex);
        }

        return resultList;
    }

    /**
     * 上下方单个
     */
    private static List<Integer> getType5(int[][] arrArr, int direction, int x, int y) {
        List<Integer> resultList = new ArrayList<>();

        // 参数只能是上或右
        if (direction > 2) {
            direction -= 2;
        }

        if (direction == 1) {
            // 上下
            int index1 = getIndexByXy(arrArr, x, y + 1);
            int index2 = getIndexByXy(arrArr, x, y - 1);
            if (index1 != -1) resultList.add(index1);
            if (index2 != -1) resultList.add(index2);
        } else {
            // 左右
            int index1 = getIndexByXy(arrArr, x - 1, y);
            int index2 = getIndexByXy(arrArr, x + 1, y);
            if (index1 != -1) resultList.add(index1);
            if (index2 != -1) resultList.add(index2);
        }

        return resultList;
    }

    /**
     * 自身所在单列（不含自身）
     */
    private static List<Integer> getType6(int[][] arrArr, int direction, int x, int y) {
        List<Integer> resultList = new ArrayList<>();

        // 参数只能是上或右
        if (direction > 2) {
            direction -= 2;
        }

        // 垂直、水平
        int[] beginXArr = {x, -1};
        int[] beginYArr = {-1, y};
        int[] changeXArr = {0, 1};
        int[] changeYArr = {1, 0};

        // 自己
        int self = getIndexByXy(arrArr, x, y);

        // 遍历单列
        int tempX = beginXArr[direction - 1];
        int tempY = beginYArr[direction - 1];
        for (int i = 0; i < 100; i++) {
            tempX += changeXArr[direction - 1];
            tempY += changeYArr[direction - 1];
            int tempIndex = getIndexByXy(arrArr, tempX, tempY);
            if (tempIndex == -1) {
                break;
            }
            if (tempIndex != self) { // 跳过自己
                resultList.add(tempIndex);
            }
        }

        return resultList;
    }

    /**
     * 全屏（不含自身）
     */
    private static List<Integer> getType7(int[][] arrArr, int index) {
        List<Integer> resultList = new ArrayList<>();

        int size = arrArr.length * arrArr[0].length;
        for (int i = 0; i < size; i++) {
            if (i != index) {
                resultList.add(i);
            }
        }

        return resultList;
    }

}
